﻿using System.Collections.Generic;
using System.Threading;
using com.fukefu.sql;
using com.fukefu.tools;

namespace com.fukefu.update.instantiated
{
    public class UpdateStatusCode : UpdateData
    {
        SqlServerOperate sql = new SqlServerOperate();
        Queue<WebUrl> queue = new Queue<WebUrl>();

        public override string GetTitle()
        {
            return "更新发布网址状态码";
        }

        public override void Start()
        {
            Display("----初始化,加载所有网址----");
            Init();
            Display("----初始化结束,更新开始----");
            SetMaxNum(queue.Count);
            mt.Begin(60, new ThreadStart(UpdateOneWebUrl));
            Display("全部更新结束");
        }
        //初始化
        private void Init()
        {
            SqlServerOperate sql = new SqlServerOperate();
            System.Data.SqlClient.SqlDataReader re = sql.Query("select * from 已发布 where statuscode!=200");
            while (re.Read())
            {
                WebUrl web = new WebUrl();
                web.Uid = re["fid"].ToString();
                web.Url = re["url"].ToString();
                queue.Enqueue(web);
            }
            re.Close();
        }
        //更新一条网址
        private void UpdateOneWebUrl()
        {
            while (true)
            {
                WebUrl web;
                lock (queue)
                {
                    if (queue.Count == 0)
                    {
                        return;
                    }
                    web = queue.Dequeue();
                }
                int code = source.GetHttpStatusCode(web.Url);
                sql.Update("update 已发布 set statuscode=" + code + " where fid='" + web.Uid + "'");
                Display("----" + web.Uid + "----");
                SetNumAdd();
            }
        }
    }
}
